package jp.co.dailyReport.project.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.project.PjStaff;

/**
 * プロジェクト参加社員情報を取得するクラスです。
 */
public class PjStaffSIdLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(PjStaffSIdLoadExecuter.class);
	private int staffId;

	public PjStaffSIdLoadExecuter(int staffId) {
		this.staffId = staffId;
	}

	/**
	 * 社員IDを基に、プロジェクト参加社員情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return プロジェクト参加社員オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select DISTINCT projectId from pjStaff where staffId=? and delFlg=0 order by projectId";
		log.debug("sql: " + sql);
		Vector answer = new Vector();

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, staffId);
			PjStaff pjStaff;

			for(rset = pstmt.executeQuery(); rset.next(); answer.add(pjStaff)) {
				pjStaff = new PjStaff();
				pjStaff.setPjStaffId(rset.getInt("pjStaffId"));
				pjStaff.setProjectId(rset.getInt("projectId"));
				pjStaff.setStaffId(rset.getInt("staffId"));
				pjStaff.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				pjStaff.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (PjStaff[])answer.toArray(new PjStaff[0]);
	}

}
